package servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/servletB")
public class ServletB extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        String key2 = (String) req.getAttribute("key2");
//        System.out.println(key2);
//        String key2 = req.getParameter("key2");
//        System.out.println(key2);
//        System.out.println("ServletB");
//
//
//        HttpSession session = req.getSession();
//        String username = (String) session.getAttribute("username");
//        System.out.println(username);

//        ServletContext servletContext = getServletContext();
//        servletContext.setAttribute("key1", "value1");
//        servletContext.setAttribute("key2", "value2");

        // 获取session
//        HttpSession session = req.getSession();
//        session.setAttribute("attr", "value");
//        session.setAttribute("attr1", "value1");

        req.setAttribute("attr", "value");
        req.setAttribute("attr1", "value1");

        // 修改
        req.setAttribute("attr", "value3");

        req.removeAttribute("attr1");
    }
}
